package org.kl.bpm.service.conf;

import java.util.List;
import java.util.Map;

import org.kl.bf.utils.CRUDUtils;
import org.kl.bpm.entity.conf.BpmConfNotice;
import org.kl.bpm.repository.conf.BpmConfNoticeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

/**
 * 
 * @Author hqm
 * @CreateTime 2014年5月25日 下午8:40:29
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class BpmConfNoticeService {

	private BpmConfNoticeDao bpmConfNoticeDao;

	public BpmConfNotice getBpmConfNotice(String id) {
		return bpmConfNoticeDao.findOne(id);
	}

	public List<BpmConfNotice> findByCodeAndProcessDefinitionId(String code, String processDefinitionId) {
		return bpmConfNoticeDao.findByBpmConfNodeCodeAndBpmConfNodeBpmConfBaseProcessDefinitionId(code, processDefinitionId);
	}

	public void saveBpmConfNotice(BpmConfNotice entity) {
		bpmConfNoticeDao.save(entity);
	}

	public void deleteBpmConfNotice(String id) {
		bpmConfNoticeDao.delete(id);
	}

	public List<BpmConfNotice> getAllBpmConfNotice() {
		return bpmConfNoticeDao.findAll();
	}
	//带条件的查询
	public List<BpmConfNotice> searchAll(Map<String, Object> searchParams, String sortType, String orderType) {
		List<BpmConfNotice> list = bpmConfNoticeDao.searchAll(searchParams, sortType, orderType, BpmConfNotice.class);
		return list;
	}
	
	public Page<BpmConfNotice> getUserBpmConfNotice(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<BpmConfNotice> spec = buildSpecification(searchParams);

		return bpmConfNoticeDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<BpmConfNotice> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<BpmConfNotice> spec = DynamicSpecifications.bySearchFilter(filters.values(), BpmConfNotice.class);
		return spec;
	}

	@Autowired
	public void setBpmConfNoticeDao(BpmConfNoticeDao bpmConfNoticeDao) {
		this.bpmConfNoticeDao = bpmConfNoticeDao;
	}

}
